from flask import Flask, Blueprint, request, jsonify
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import warnings
import os
from scipy.sparse import csr_matrix
import pandas as pd

intent_recognition_bp = Blueprint('intent_recognition', __name__)# 组织相关的路由和视图函数
warnings.filterwarnings('ignore')
app = Flask(__name__)

#获取当前脚本所在的目录，
current_dir = os.path.dirname(os.path.abspath(__file__))
file1_path = os.path.join(current_dir, 'model', 'baike_qa_train.json')
file2_path = os.path.join(current_dir, 'model', 'baike_qa_valid.json')

# 函数从json文件中逐行加载数据
def load_data(file_path):
    data = []
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            try:
                json_obj = json.loads(line.strip())
                data.append(json_obj)
            except json.JSONDecodeError as e:
                print(f"Error loading JSON from line: {line}")
                continue
    return data

# 加载和处理数据的功能
def load_and_process_data():
    qa_data1 = load_data(file1_path)
    qa_data2 = load_data(file2_path)
    qa_data = qa_data1 + qa_data2
    questions = [item['title'] for item in qa_data]

    vectorizer = TfidfVectorizer().fit(questions)#将问题转换为TF-IDF向量，最后返回处理后的数据
    vectors_sparse = vectorizer.transform(questions)

    return qa_data, questions, vectorizer, vectors_sparse

qa_data, questions, vectorizer, vectors_sparse = load_and_process_data()

@intent_recognition_bp.route('/api/qa', methods=['GET'])
def get_qa():
    return jsonify(qa_data)

@intent_recognition_bp.route('/api/qa/search', methods=['POST'])
def search_qa():
    user_question = request.json.get('question')
    if not user_question:
        return jsonify({"error": "问题不能为空"}), 400

    user_vector_sparse = vectorizer.transform([user_question])
    cosine_similarities = cosine_similarity(user_vector_sparse, vectors_sparse).flatten()
    most_similar_index = cosine_similarities.argmax()
    most_similar_question = questions[most_similar_index]
    answer = qa_data[most_similar_index]['answer']

    return jsonify({"question": most_similar_question, "answer": answer})

# 用于创建应用程序的函数
def create_app():
    app.register_blueprint(intent_recognition_bp, url_prefix='/intent_recognition')
    return app

